import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/*
 * accSecGrpAuthorityCompany - 子账户管理-分配安全组-数据权限tab页 - 公司 - model
 * @date: 2019-12-23

 */
import { getResponse } from 'utils/utils';
import { queryCompanyPermission } from "../../services/subAccountOrgService";
export default {
  namespace: 'accSecGrpAuthorityCompany',
  state: {
    data: [],
    checkList: [],
    originList: [],
    expandedRowKeys: []
  },
  effects: {
    fetchAuthorityCompanyAndExpand: /*#__PURE__*/_regeneratorRuntime.mark(function fetchAuthorityCompanyAndExpand(_ref, _ref2) {
      var payload, call, put, response, data, expandedRowKeys;
      return _regeneratorRuntime.wrap(function fetchAuthorityCompanyAndExpand$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(queryCompanyPermission, payload);

            case 4:
              response = _context.sent;
              data = getResponse(response);

              if (!data) {
                _context.next = 12;
                break;
              }

              expandedRowKeys = data.originList && data.originList.map(function (list) {
                return list.id;
              });
              _context.next = 10;
              return put({
                type: 'queryCompany',
                payload: data
              });

            case 10:
              _context.next = 12;
              return put({
                type: 'updateExpanded',
                payload: expandedRowKeys
              });

            case 12:
            case "end":
              return _context.stop();
          }
        }
      }, fetchAuthorityCompanyAndExpand);
    })
  },
  reducers: {
    queryCompany: function queryCompany(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        data: action.payload.treeList,
        checkList: action.payload.originList.filter(function (list) {
          return list.checkedFlag === 1;
        }),
        originList: action.payload.originList
      });
    },
    updateExpanded: function updateExpanded(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        expandedRowKeys: action.payload
      });
    },
    updateState: function updateState(state, _ref3) {
      var payload = _ref3.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};